//
//  LEReSiteView.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/16.
//  Copyright © 2017年 iOSer. All rights reserved.
//

#import "LEReSiteView.h"
#import "GGAppDelegate.h"
@interface LEReSiteView()
@property(nonatomic,copy) NSArray *itemArray;
@property(nonatomic,copy) NSMutableArray *btnArray;
@end
@implementation LEReSiteView

+(instancetype)LEReSiteViewWithArray:(NSArray *)Array delegate:(id<LEReSiteViewDelegate>)delegate{
    return [[self alloc] initWithArray:Array andDelegate:delegate];
}

-(instancetype)initWithArray:(NSArray *)array andDelegate:(id<LEReSiteViewDelegate>)delegate{
    self.itemArray = array;
    _delegate = delegate;
    return [self initActionSheet];
}


-(instancetype)initActionSheet{
    if (self = [super init]) {
        self.hidden = YES;
        GGAppDelegate * appDelegate = (GGAppDelegate *)[UIApplication sharedApplication].delegate;
        self.frame = [appDelegate window].frame;
        [self setBackgroundColor:HEXColor(0x000000, 0)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
        [self addGestureRecognizer:tap];
//        self.transform = CGAffineTransformMakeRotation(-M_PI_2);
//        self.frame = CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH);
        
        for (int i = 0; i<self.itemArray.count; ++i) {
            UIButton *btn = [UIButton new];
            [btn setTitleColor:HEXColor(0x000000, 1) forState:UIControlStateNormal];
            [btn setBackgroundColor:HEXColor(0xffffff, 1)];
            [btn setTitle:self.itemArray[i] forState:UIControlStateNormal];
            btn.tag = 990+i;
            btn.layer.borderColor = HEXColor(0x000000, 1).CGColor;
            btn.layer.borderWidth = 0.5;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
//            btn.frame = CGRectMake(20, SCREEN_HEIGHT-GGRealValue(5), (SCREEN_WIDTH-100)/4, GGRealValue(20));
            [btn makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self).offset(-GGRealValue(5));
                make.left.equalTo(self).offset(20);
                make.size.equalTo(CGSizeMake((SCREEN_WIDTH-100)/4, GGRealValue(20)));
            }];
            [self.btnArray addObject:btn];
        }
    }
    return self;
}

-(void)btnClick:(UIButton *)btn{
    [self dismissView];
    [self.delegate reSiteClickBtnWithIndex:btn.tag-990];
}
-(void)showLERESiteView{
    self.hidden = NO;
//    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [appDelegate.window addSubview:self];
    self.layer.borderColor = [UIColor yellowColor].CGColor;
    self.layer.borderWidth = 1;

    for (int i = 0; i<self.btnArray.count; ++i) {
        UIButton *btn = self.btnArray[i];
        btn.hidden = NO;
        [self.superview layoutIfNeeded];
        [UIView animateWithDuration:0.3 animations:^{
            
            [btn updateConstraints:^(MASConstraintMaker *make) {
                
               make.bottom.equalTo(self).offset(-((GGRealValue(20)*(i+1)+GGRealValue(5))));
            }];
            [self.superview layoutIfNeeded];//强制绘制
        }];
//        btn.frame = CGRectMake(20, SCREEN_HEIGHT-(GGRealValue(20)*(i+1)+GGRealValue(5)), (SCREEN_WIDTH-100)/4, GGRealValue(20));
    }
    
}


-(void)dismissView{
    
    
    for (NSUInteger i = self.btnArray.count; i > 0; --i){
        
        UIButton *btn = self.btnArray[i-1];
        
        
        [UIView animateWithDuration:0.3 animations:^{
            [btn updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.bottom.equalTo(self).offset(-GGRealValue(5));
                }];
                [self.superview layoutIfNeeded];//强制绘制
            
        } completion:^(BOOL finished) {
            self.hidden = YES;
            self.disBlock();
        }];
        
    }
    
}

-(NSMutableArray *)btnArray{
    if (!_btnArray) {
        _btnArray = [[NSMutableArray alloc] initWithCapacity:42];
    }
    return _btnArray;
}
@end
